import time, logging
from queue import Queue
from WechatPCAPI import WechatPCAPI


# 机器人
class WechatChatbot():
    def __init__(self, **kwargs):
        logging.basicConfig(level=logging.INFO)
        self.pc_api = WechatPCAPI(on_message=self.listen, log=logging)
        self.listen_queue = Queue()

    # 外部调用
    def run(self):
        self.pc_api.start_wechat(block=True)
        while not self.pc_api.get_myself():
            time.sleep(5)
        print('[INFO]: 登录成功, 开始自动聊天...')
        while True:
            msg = self.listen_queue.get()
            print(msg)
            # 先登录一次  看看自己的群聊id是多少  xxxxxxxxx@chatroom (xx是数字)
            """
            cc = str(msg)#转文本过滤登录信息
            if 'msg::chatroom' in cc:
                    print('检测所在群有消息，正在进行提取')
                    sender = msg['data']['msgfrominfo']['wx_id']  # 提取发送群id
                    tomsg = msg['data']['msgcontent']        # 提取发送的信息,不能转发图片视频应该
                    sendid = msg['data']['sendid']#指定用户
                    print(sender)
                    print(tomsg)
                    if sender == '2222291416@chatroom': #指定某个群内某个人的话下面还要加个条件判断
                            if sendid == 'wxid_54234lsujtx22':#指定用户
                                    self.pc_api.send_text(to_user='wxid_54123343jtx22', msg=tomsg)#发送给接收人或者群
                                    print("转发消息成功")
                            else:
                                    print("不符合转发人条件")
                    else:
                            print("群不符合转发消息条件")
            else:
                    print('未检测监测群有消息')

            """

    # 消息监听
    def listen(self, msg):
        self.listen_queue.put(msg)


if __name__ == '__main__':
    client = WechatChatbot()
    client.run()